var username = getCookie("username");
var li = document.querySelector(".header-con li:first-child");
// console.log(li);
if (username) {
    li.innerHTML = "";
    var str = `
            欢迎${username}光临
            <a href="javascript:;" class="del">退出</a>
    `;
    li.innerHTML = str;
    var del = document.querySelector(".del");
    del.onclick = function () {
        removeCookie("username");
        li.innerHTML = "";
        var str = `
            <a href="login.html" id="login">登陆</a>
            <a href="register.html" id="register">注册</a>
        `;
        li.innerHTML = str;
    }
}


$(function () {
    var flag = true;
    var topR = $(".recoganize").offset().top;
    // 电梯导航
    $(".lift_nav ul").on("click", "li", function () {
        flag = false;
        $("html,body").stop().animate({
            scrollTop: $(".cart-item").children().eq($(this).index()).offset().top
        }, 300, function () {
            flag = true;
        })
        $(this).addClass("current").siblings().removeClass("current");
    })

    showLift(); // 重新刷新页面的时候执行一次该函数确定是否显示电梯导航，不然显示电梯导航在滚动事件中，刷新页面没有滚动页面就始终不会显示电梯导航

    addColor(); // 页面加载的时候需要判断当前页面处于什么模块

    $(window).on("scroll", function () {
        showLift();
        // 处理滑动导航
        if (flag) {
            addColor()
        }
        // 处理到达顶部
        if ($(document).scrollTop() >= $(".fashion").offset().top) {
            $(".toTop").stop().animate({
                bottom: 228
            }, 200)
        } else {
            $(".toTop").stop().animate({
                bottom: 50
            })
        }

        // 处理滑动导航
        if (document.documentElement.clientWidth <= parseInt($("html").css("min-width"))) {
            $(".lift_nav,.toTop").hide();
        }

        // 处理华滑动电梯
        solveLift();

        // 为你推荐
        var topW = document.documentElement.scrollTop || document.body.scrollTop;
        var minUl = getMinUl();

        if (topW > $(".fashion").offset().top + minUl.clientHeight) {
            // console.log(111);
            foryou(start);
        }

    })

    var start = 0;
    var index;
    var mark = true;
    foryou(start);

    // 加载为你推荐的数据
    function foryou() {
        // console.log(mark); // 如果不发送请求的话说明标志是false或者是undefined
        if (!mark) {   // 直到他请求到数据的时候才允许发送下次请求
            return false;
        }
        mark = false;
        // 为你推荐
        $.ajax({
            url: "./serve/list.php",
            method: "post",
            dataType: "json",
            beforeSend:function() {
                index = layer.load(0,{shade: false});
            },
            success: function (res) {
                layer.close(index);
                // console.log(res);
                res = res.slice(start, start + 8);
                start = start + 8;
                // console.log(res);
                if (res.length == 0) {   // 如果数组里面的内容已经取完，那么停止
                    return false;
                }
                for (var i = 0; i < res.length; i++) {
                    str = `
                        <li>
                            <img src = "${res[i].imgpath}" alt="">
                            <p>${res[i].name}</p>
                            <p>${res[i].introduce}</p>
                            <span>￥${res[i].price}元</span>
                            <p>
                                <a href="detail.html?id=${res[i].id}">详情</a>
                            </p>
                        </li>
                    `;
                    var minUl = getMinUl(); // 先获取数据在往最小的ul中添加数据
                    minUl.innerHTML += str;
                }
                mark = true;
            }
        })
    }

    function solveLift() {
        if (document.documentElement.clientWidth <= parseInt($("html").css("min-width"))) {
            $(".lift_nav,.toTop").hide();
        }
    }

    function addColor() {
        $.each($(".cart-item").children(), function (i, ele) {
            if ($(document).scrollTop() >= $(ele).offset().top) {
                $(".lift_nav li").eq(i).addClass("current").siblings().removeClass("current");
            }
        })
    }


    function showLift() {
        if ($(document).scrollTop() >= topR) {
            $(".lift_nav").stop().fadeIn(200, function () {
                $(".lift_nav").stop().animate({
                    top: 70
                })
            });
            $(".searchbox").stop().slideDown(200);
        } else {
            $(".lift_nav").stop().hide();
            $(".searchbox").stop().slideUp(200);
        }
    }

    // 返回顶部
    $(".toTop").on("click", function () {
        $("html,body").stop().animate({
            scrollTop: 0
        }, 300)
    })

    function getMinUl() {
        var uls = document.querySelectorAll(".foryou ul");
        var minUl = uls[0];
        for (var i = 0; i < uls.length; i++) {
            if (uls[i].clientHeight < minUl.clientHeight) {
                minUl = uls[i];
            }
        }
        return minUl;
    }


    $(window).on("resize", function () {
        solveLift();
    })

    // 商品广告
    $(".goods_adv li").on("mouseover", function () {
        $(this).addClass("toggle").siblings().removeClass("toggle");
    })

})

// 移动广告
var timerId = null;
var moveGoods = document.querySelector(".move_goods");
var goodsTab = document.querySelector(".goods_tab");
var srcoll = document.querySelector(".scroll");
var scrollBar = document.querySelector(".scrollBar");
var x = parseFloat(getComputedStyle(goodsTab).transform.substring(7).split(",")[4]);
// console.log(x);
moveGoods.onselectstart = function () {
    return false;
}
autoMove();
function autoMove() {
    timerId = setInterval(() => {
        x += 0.1;
        if (x > parseFloat(getComputedStyle(moveGoods).width)) {
            x = 0;
        }
        goodsTab.style.transform = "translate3d(-" + x + "px,0px,0px)"
    }, 1);
}
moveGoods.onmouseover = function () {
    clearInterval(timerId);
    srcoll.style.visibility = "visible";
}
moveGoods.onmouseout = function () {
    autoMove();
    srcoll.style.visibility = "hidden";
}
scrollBar.onmousedown = function () {
    scrollBar.onmousemove = function (e) {
        var e = e || window.event;
        var lmove = e.pageX - moveGoods.offsetLeft - this.clientWidth / 2;
        if (lmove >= parseFloat(getComputedStyle(moveGoods).width) - this.clientWidth || lmove <= 0) {
            scrollBar.onmousemove = null;
        }
        scrollBar.style.left = lmove + "px";
        goodsTab.style.transform = "translate3d(-" + lmove + "px,0px,0px)";
    }
}

// 倒计时
var time_round = new Date();
var now_hour = time_round.getHours();
var future_hour = now_hour + 1;
// 每隔一小时改一次时间
var hour_timerId = setInterval(function () {
    future_hour += 1;
}, 3600 * 1000);
future_hour = future_hour < 10 ? "0" + future_hour + ":00" : "" + future_hour + ":00";

// 距离现在一小时后的时间
function updateActive() {
    var active_time = new Date();
    var active_year = active_time.getFullYear();
    var active_month = active_time.getMonth();
    var active_day = active_time.getDate();
    var active_hour = active_time.getHours();
    var active_minute = active_time.getMinutes();
    var active_second = active_time.getSeconds();
    active_minute = 0;
    active_second = 0;
    active_hour = active_hour + 1;
    active_time = new Date(`${active_year}-${active_month + 1}-${active_day} ${active_hour}:${active_minute}:${active_second}`);
    // active_time = active_time.getTime();
    return active_time;
}
var active_time = updateActive();
var active_timerId = setInterval(function () {
    active_time = updateActive();
}, 3600 * 1000)


countTime();
function countTime() {
    var now_time = +new Date();
    var diff_time = active_time - now_time;
    var hour = parseInt(diff_time / 1000 / 60 / 60 % 24);
    hour = hour < 10 ? '0' + hour : '' + hour;
    var minute = parseInt(diff_time / 1000 / 60 % 60);
    minute = minute < 10 ? '0' + minute : '' + minute;
    var second = parseInt(diff_time / 1000 % 60);
    second = second < 10 ? '0' + second : '' + second;
    // console.log(future_hour, hour, minute, second);
    var roundtime = document.querySelector(".time_festival strong");
    roundtime.innerText = future_hour;
    var timehour = document.querySelector(".time_hour");
    var timeminute = document.querySelector(".time_minute");
    var timesecond = document.querySelector(".time_second");
    timehour.innerText = hour;
    timeminute.innerText = minute;
    timesecond.innerText = second;
}
setInterval(countTime, 1000);